package org.jeecg.modules.fund.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.beanutils.locale.LocaleConvertUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.front.util.FundDate;
import org.jeecg.modules.fund.entity.UserhomeFund;
import org.jeecg.modules.fund.entity.UserhomeOrder;
import org.jeecg.modules.fund.entity.UserhomeWalletFundLog;
import org.jeecg.modules.fund.entity.UserhomeWalletLog;
import org.jeecg.modules.fund.mapper.UserhomeFundMapper;
import org.jeecg.modules.fund.mapper.UserhomeWalletFundLogMapper;
import org.jeecg.modules.fund.mapper.UserhomeWalletLogMapper;
import org.jeecg.modules.fund.service.IUserhomeFundService;
import org.jeecg.modules.gongqiang.entity.Userhome;
import org.jeecg.modules.gongqiang.mapper.UserhomeMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: 用户基金
 * @Author: jeecg-boot
 * @Date: 2024-08-29
 * @Version: V1.0
 */
@Service
public class UserhomeFundServiceImpl extends ServiceImpl<UserhomeFundMapper, UserhomeFund> implements IUserhomeFundService {

    @Resource
    private UserhomeMapper userhomeMapper;

    @Resource
    private UserhomeWalletFundLogMapper userhomeWalletFundLogMapper;

    @Resource
    private  UserhomeWalletLogMapper userhomeWalletLogMapper;

    @Resource
    private UserhomeFundMapper userhomeFundMapper;

    @Override
    public IPage<UserhomeFund> pageList(Page<UserhomeFund> page, QueryWrapper<UserhomeFund> queryWrapper) {
        return this.baseMapper.getPageList(page, queryWrapper);
    }

    //算利息
    @Override
    public void profit(UserhomeFund userhomeFund) {
        //自己的基金利息
        double profitRateStandard = 0.015;
        double profitRate = 0;
        if (FundDate.isOne(userhomeFund.getProfitDay(), new Date())) {
            profitRate = profitRateStandard;
        } else {
            profitRate = profitRateStandard / 30 * FundDate.daysBetween(userhomeFund.getProfitDay(), new Date());
        }
        double profitMoney = FundDate.DecimalFormatTwo(profitRate * userhomeFund.getCanRedeemNum());
        if (profitMoney >= 0.01) {
            //基金钱包加钱

            Userhome selfUserHome = userhomeMapper.selectById(userhomeFund.getUserhomeId());
            selfUserHome.setFMoney(profitMoney+selfUserHome.getFMoney());
            userhomeMapper.updateById(selfUserHome);
            //基金利息修改记录
            userhomeFund.setProfitMoney(userhomeFund.getProfitMoney()+profitMoney);
            userhomeFund.setProfitDay(new Date());
            userhomeFundMapper.updateById(userhomeFund);
            //流水记录
            UserhomeWalletFundLog userhomeWalletFundLog=new UserhomeWalletFundLog();
            userhomeWalletFundLog.setUserhomeId(selfUserHome.getId());
            userhomeWalletFundLog.setAmount(profitMoney);
            userhomeWalletFundLog.setType(2);
            userhomeWalletFundLog.setSmoney(selfUserHome.getFMoney());
            userhomeWalletFundLog.setRelateOrderNum(userhomeFund.getOrderNum());
            userhomeWalletFundLogMapper.insert(userhomeWalletFundLog);
            //推荐人利息
            if (oConvertUtils.isNotEmpty(selfUserHome.getInviterId())) {
                Userhome recommendUserHome = userhomeMapper.selectById(selfUserHome.getInviterId());
                if (oConvertUtils.isNotEmpty(recommendUserHome)) {
                    double recommendRate = 0.4;
                    recommendRate = recommendRate * profitRate;
                    double recommendMoney = FundDate.DecimalFormatTwo(recommendRate * userhomeFund.getCanRedeemNum());
                    if (recommendMoney >=0.01) {
                        //钱包加钱
                        recommendUserHome.setMoney(recommendMoney+recommendUserHome.getMoney());
                        userhomeMapper.updateById(recommendUserHome);
                        //流水记录
                        UserhomeWalletLog userhomeWalletLog=new UserhomeWalletLog();
                        userhomeWalletLog.setUserhomeId(recommendUserHome.getId());
                        userhomeWalletLog.setAmount(recommendMoney);
                        userhomeWalletLog.setType(2);
                        userhomeWalletLog.setSmoney(recommendUserHome.getMoney());
                        userhomeWalletLog.setRelateOrderNum(userhomeFund.getOrderNum());
                        userhomeWalletLogMapper.insert(userhomeWalletLog);
                    }
                }
            }
        }


    }

}
